<?php

namespace app\common\model;

use app\api\service\R;
use think\Db;
use think\Model;

class StCard extends Model
{
    protected $name = "st_card";
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $hidden=['','comid'];
    static $cardType = ['FC'=>'消费卡','CC'=>'次数卡'];

    public function cardInfo()
    {
        return $this->hasOne(StCardInfo::class, 'mid', 'id');
    }

    public function syncard($data){
        $boolf = false;
        Db::startTrans();
        try {
            $type= $data['card_type'];
            $tam = $data['total_amount'];
            $tci = $data['total_cishu'];
            $st  = $data['valid_start_time'];
            $et  = $data['valid_end_time'];

            if (isset($data['id']) && ($tam||$tci)) {
                $idsArray = explode(',', $data['id']);
                unset($data['id']);
                foreach ($idsArray as $id) {
                    $data['mid'] = $id;
                    $map = [
                        ['b.mid', '=', $id],
                        ['b.valid_start_time', '<=', $st],
                        ['b.valid_end_time', '>=', $et],
                    ];
                    $re = StCardInfo::where($map)->alias('b')
                        ->rightJoin("$this->name a", 'a.id = b.mid')
                        ->field('b.*, a.card_type')->find();

                    //次卡
                    if ($type == "CC"){
                        if ($re && $re['card_type']==$type) $boolf = $re->setInc('total_cishu', $tci);
                        if ($boolf) continue;
                        StCardInfo::create($data);
                    }
                    //消费卡
                    if ($type == "FC") {
                        if ($re && $re['card_type']==$type) $boolf = $re->setInc('total_amount', $tam);
                        if ($boolf) continue;
                        StCardInfo::create($data);
                    }

                }
            }else{
                $res = self::create($data);
                $res->cardInfo()->save($data);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }



    //收银支付
    public  function ApiPayment($payType,$cardCode,$amount=0,$isRes=null){
        $date = date('Y-m-d',time());
        $card = self::where(['card_code'=>$cardCode, 'status'=>1])->find();
        if (!$card || $card['comid']!=COMID) return R::error('无效码');
        if ($card['card_type']!=$payType) return R::error('该卡为'.self::$cardType[$card['card_type']]);
        Db::startTrans();
        try {

            if ($isRes){
                $res = StReservation::ApiGetReservaByDate($card['uid']);
               //dump($res);exit();
                if (!$res) return R::error('未预定餐');
                if ($res['attr_status']!=0) return R::error('该时间段已无预定记录');
            }

            $map = [
                ['a.status', '=', 1],
                ['a.card_type', '=', $payType],
                ['a.card_code', '=', $cardCode],
                ['b.valid_start_time', '<=', $date],
                ['b.valid_end_time', '>=', $date],
            ];
            $row = StCardInfo::where($map)->alias('b')
                ->rightJoin("$this->name a", 'a.id = b.mid')
                ->field('a.card_type , b.*')->find();
            if (!$row) return R::error('未充值卡');
            //次卡
            if ($row && $payType == "CC"){
                if ($row['total_cishu']<=0) return R::error('次数不足');
                $boolf = $row->setDec('total_cishu', 1);
            }
            //消费
            if ($row && $payType == "FC"){
                if ($row['total_amount']<$amount) return R::error('余额不足');
                $boolf = $row->setDec('total_amount', $amount);
            }
            if ($isRes)  $boolf = StReservationAttr::where(['id'=>$res['id']])->update(['attr_status'=>1,'update_time'=>time()]);
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
            return R::error($e->getMessage());
            //return $e->getMessage();
        }

        return R::success('支付成功');
    }



}
